package controller_user

import (
	"github.com/gin-gonic/gin"
	"github.com/gin-gonic/gin/binding"
	"tools-api/src/common/utils"
	"tools-api/src/jcj/model"
	"tools-api/src/jcj/service/service_book"
	"tools-api/src/jcj/service/service_user"
	"tools-api/src/routes/request"
	"tools-api/src/routes/response"
)

// GetUserInfo 获取用户信息
func GetUserInfo(ctx *gin.Context) {
	uuid := request.GetCurrentUser(ctx)
	user := service_user.GetUser(uuid)
	result := getUserInfo(user)
	response.Success(ctx, result)
}

// getUserInfo 用户信息返回 登录接口、单独获取用户信息接口
func getUserInfo(user model.User) gin.H {
	user.Phone = utils.HidePhone(utils.Decrypt(user.Phone))
	defAccount := service_book.GetDefaultAccount(user.Uuid) // 默认账户
	currentBook := service_book.GetCurrentBook(user.Uuid)   // 当前账本
	return gin.H{"user": user, "ext": gin.H{"book_current": currentBook, "accounts_def": defAccount}}
}

// MfaEnable MFA开关
func MfaEnable(ctx *gin.Context) {
	var req = struct {
		Enable bool `json:"enable"`
	}{}
	if err := ctx.ShouldBindBodyWith(&req, binding.JSON); err != nil {
		response.Fail(ctx, "参数错误")
		return
	}
	uuid := request.GetCurrentUser(ctx)
	err := service_user.EnableMfa(uuid, req.Enable)
	if err != nil {
		response.Fail(ctx, "修改失败")
		return
	}
	response.SuccessMsg(ctx, "修改成功")
}

// MfaReset 重置mfa
func MfaReset(ctx *gin.Context) {
	// 需要验证手机号 或者 检查常用设备
}
